package shardkv

import "net"
import "fmt"
import "net/rpc"
import "log"
import "time"
import "paxos"
import "sync"
import "sync/atomic"
import "os"
import "syscall"
import "encoding/gob"
import "math/rand"
import "shardmaster"

const Debug = 1

func DPrintf(format string, a ...interface{}) (n int, err error) {
	if Debug > 0 {
		log.Printf(format, a...)
	}
	return
}

type Op struct {
	// Your definitions here.

	Seq   int64
	Op    string
	Key   string
	Value string

	Data   map[string]string
	SeqMap map[int64]bool
	Num    int
}

type ShardKV struct {
	mu         sync.Mutex
	l          net.Listener
	me         int
	dead       int32 // for testing
	unreliable int32 // for testing
	sm         *shardmaster.Clerk
	px         *paxos.Paxos

	gid int64 // my replica group ID

	// Your definitions here.

	data       map[string]string
	seqMap     map[int64]bool
	lastConfig shardmaster.Config
	lastNum    int
}

func (kv *ShardKV) Get(args *GetArgs, reply *GetReply) error {
	// Your code here.
	kv.mu.Lock()
	defer kv.mu.Unlock()

	if !kv.inShard(args.Key) || args.Num > kv.lastConfig.Num {
		reply.Err = ErrWrongGroup
		return nil
	}

	kv.px.MakePropose(Op{Seq: nrand(), Op: "Get", Key: args.Key})
	value, ok := kv.data[args.Key]
	if ok {
		reply.Err = OK
		reply.Value = value
	} else {
		reply.Err = ErrNoKey
	}
	return nil
}

// RPC handler for client Put and Append requests
func (kv *ShardKV) PutAppend(args *PutAppendArgs, reply *PutAppendReply) error {
	// Your code here.

	kv.mu.Lock()
	defer kv.mu.Unlock()

	if !kv.inShard(args.Key) || args.Num > kv.lastConfig.Num {
		reply.Err = ErrWrongGroup
		return nil
	}

	reply.Err = OK
	if _, ok := kv.seqMap[args.Seq]; ok {
		return nil
	}

	kv.px.MakePropose(Op{Seq: args.Seq, Op: args.Op, Key: args.Key, Value: args.Value})
	return nil
}

func (kv *ShardKV) handle(obj interface{}) {
	op := obj.(Op)
	switch op.Op {
	case "Get":
	case "Put":
		kv.data[op.Key] = op.Value
	case "Append":
		kv.data[op.Key] += op.Value
	case "Join":
		if op.Num > kv.lastConfig.Num {
			kv.lastNum = op.Num
		}
	}
	kv.seqMap[op.Seq] = true
}

func checkOp(a, b interface{}) bool {
	return a.(Op).Seq == b.(Op).Seq
}

func (kv *ShardKV) Join(args *JoinArgs, reply *JoinReply) error {

	kv.mu.Lock()
	defer kv.mu.Unlock()

	kv.px.MakePropose(Op{Seq: nrand(), Op: "Join", Num: args.Num})

	reply.SeqMap = make(map[int64]bool)
	for k, v := range kv.seqMap {
		reply.SeqMap[k] = v
	}

	reply.Data = make(map[string]string)
	for k, v := range kv.data {
		if _, ok := args.Shards[key2shard(k)]; ok {
			reply.Data[k] = v
		}
	}
	return nil
}

//
// Ask the shardmaster if there's a new configuration;
// if so, re-configure.
//
func (kv *ShardKV) tick() {
	config := kv.sm.Query(kv.lastConfig.Num + 1)
	if config.Num == kv.lastConfig.Num && kv.lastConfig.Num == kv.lastNum {
		return
	}

	newShardSlice := config.GetShard(kv.gid)
	lastShardSlice := kv.lastConfig.GetShard(kv.gid)
	joinSlice := subSlice(newShardSlice, lastShardSlice)
	joinGid := kv.lastConfig.GetGidSlice(joinSlice, kv.gid)

	kv.mu.Lock()
	defer kv.mu.Unlock()

	args := &JoinArgs{Num: config.Num}
	for _, gid := range joinGid {
		args.Shards = make(map[int]bool)
		for _, v := range joinSlice {
			if kv.lastConfig.Shards[v] == gid {
				args.Shards[v] = true
			}
		}
		success := false
		for !success {
			for _, v := range kv.lastConfig.Groups[gid] {
				var reply JoinReply
				if !call(v, "ShardKV.Join", args, &reply) {
					continue
				}
				for k, v := range reply.Data {
					kv.data[k] = v
				}

				for k, v := range reply.SeqMap {
					kv.seqMap[k] = v
				}
				success = true
				break
			}
		}
	}
	kv.lastConfig = config.Clone()
	kv.lastNum = kv.lastConfig.Num
}

func subSlice(a, b []int) []int {
	r := make([]int, 0)

	for _, v1 := range a {
		in := false
		for _, v2 := range b {
			if v2 == v1 {
				in = true
				break
			}
		}
		if !in {
			r = append(r, v1)
		}
	}
	return r
}

func (kv *ShardKV) inShard(key string) bool {
	if kv.lastNum > kv.lastConfig.Num {
		return false
	} else {
		return kv.lastConfig.Shards[key2shard(key)] == kv.gid
	}
}

// tell the server to shut itself down.
// please don't change these two functions.
func (kv *ShardKV) kill() {
	atomic.StoreInt32(&kv.dead, 1)
	kv.l.Close()
	kv.px.Kill()
}

// call this to find out if the server is dead.
func (kv *ShardKV) isdead() bool {
	return atomic.LoadInt32(&kv.dead) != 0
}

// please do not change these two functions.
func (kv *ShardKV) Setunreliable(what bool) {
	if what {
		atomic.StoreInt32(&kv.unreliable, 1)
	} else {
		atomic.StoreInt32(&kv.unreliable, 0)
	}
}

func (kv *ShardKV) isunreliable() bool {
	return atomic.LoadInt32(&kv.unreliable) != 0
}

//
// Start a shardkv server.
// gid is the ID of the server's replica group.
// shardmasters[] contains the ports of the
//   servers that implement the shardmaster.
// servers[] contains the ports of the servers
//   in this replica group.
// Me is the index of this server in servers[].
//
func StartServer(gid int64, shardmasters []string,
	servers []string, me int) *ShardKV {
	gob.Register(Op{})

	kv := new(ShardKV)
	kv.me = me
	kv.gid = gid
	kv.sm = shardmaster.MakeClerk(shardmasters)

	// Your initialization code here.
	// Don't call Join().

	rpcs := rpc.NewServer()
	rpcs.Register(kv)

	kv.px = paxos.Make(servers, me, rpcs)
	kv.px.SetFunc(checkOp, kv.handle)

	os.Remove(servers[me])
	l, e := net.Listen("unix", servers[me])
	if e != nil {
		log.Fatal("listen error: ", e)
	}
	kv.l = l

	kv.data = make(map[string]string)
	kv.seqMap = make(map[int64]bool)

	// please do not change any of the following code,
	// or do anything to subvert it.

	go func() {
		for kv.isdead() == false {
			conn, err := kv.l.Accept()
			if err == nil && kv.isdead() == false {
				if kv.isunreliable() && (rand.Int63()%1000) < 100 {
					// discard the request.
					conn.Close()
				} else if kv.isunreliable() && (rand.Int63()%1000) < 200 {
					// process the request but force discard of reply.
					c1 := conn.(*net.UnixConn)
					f, _ := c1.File()
					err := syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
					if err != nil {
						fmt.Printf("shutdown: %v\n", err)
					}
					go rpcs.ServeConn(conn)
				} else {
					go rpcs.ServeConn(conn)
				}
			} else if err == nil {
				conn.Close()
			}
			if err != nil && kv.isdead() == false {
				fmt.Printf("ShardKV(%v) accept: %v\n", me, err.Error())
				kv.kill()
			}
		}
	}()

	go func() {
		for kv.isdead() == false {
			kv.tick()
			time.Sleep(250 * time.Millisecond)
		}
	}()

	return kv
}
